<#assign filePath>${project.projectName}/${project.projectName}-biz/src/main/java/${project.basePackage?replace(".","/")}/${model.moduleName?replace(".","/")}/controller/</#assign>
<#assign fileName>${model.name?cap_first}Controller.java</#assign>
<#if !(model.methodList?? && model.methodList?size gt 0) ><#stop></#if>
package ${project.basePackage}.${module.moduleName}.controller;

<#list model.fieldList as field>
    <#if model.primaryFieldId?? && model.primaryFieldId == field.id >
        <#assign primaryField= field>
    </#if>
</#list>
<#assign importPkgs=['lombok.RequiredArgsConstructor','com.fa4j.common.web.annotation.ApiMethod','com.fa4j.common.web.model.AppType','jakarta.validation.Valid']>
<#assign importPkgs=importPkgs+['org.springframework.web.bind.annotation.RestController','org.springframework.web.bind.annotation.RequestMapping']>
<#assign importPkgs=importPkgs+[project.basePackage+'.'+module.moduleName+'.model.'+model.name?cap_first]>
<#assign importPkgs=importPkgs+[project.basePackage+'.'+module.moduleName+'.repository.'+model.name?cap_first+"Repository"]>
<#assign importPkgs=importPkgs+[project.basePackage+'.'+module.moduleName+'.service.'+model.name?cap_first+"Service"]>
<#list model.methodList as method>
    <#if method.type == "PAGE_QUERY">
        <#assign importPkgs=importPkgs+['com.fa4j.common.base.model.Page']>
        <#assign importPkgs=importPkgs+[project.basePackage+'.'+module.moduleName+'.model.'+model.name?cap_first+method.name?cap_first+'Query']>
        <#if !importPkgs?seq_contains('org.springframework.web.bind.annotation.GetMapping')>
            <#assign importPkgs=importPkgs+['org.springframework.web.bind.annotation.GetMapping']>
        </#if>
    </#if>
    <#if method.type == "DETAIL_QUERY" && primaryField??>
        <#assign importPkgs=importPkgs+['com.fa4j.common.base.exception.CommonRequestError']>
        <#if !importPkgs?seq_contains('org.springframework.web.bind.annotation.GetMapping')>
            <#assign importPkgs=importPkgs+['org.springframework.web.bind.annotation.GetMapping']>
        </#if>
        <#if !importPkgs?seq_contains('org.springframework.web.bind.annotation.PathVariable')>
            <#assign importPkgs=importPkgs+['org.springframework.web.bind.annotation.PathVariable']>
        </#if>
    </#if>
    <#if method.type == "CREATE_CMD">
        <#assign importPkgs=importPkgs+[project.basePackage+'.'+module.moduleName+'.model.'+model.name?cap_first+method.name?cap_first+'Cmd']>
        <#if !importPkgs?seq_contains('org.springframework.web.bind.annotation.PostMapping')>
            <#assign importPkgs=importPkgs+['org.springframework.web.bind.annotation.PostMapping']>
        </#if>
        <#if !importPkgs?seq_contains('org.springframework.web.bind.annotation.RequestBody')>
            <#assign importPkgs=importPkgs+['org.springframework.web.bind.annotation.RequestBody']>
        </#if>
        <#if !importPkgs?seq_contains('jakarta.validation.Valid')>
            <#assign importPkgs=importPkgs+['jakarta.validation.Valid']>
        </#if>
    </#if>
    <#if method.type == "MODIFY_CMD" && primaryField??>
        <#assign importPkgs=importPkgs+[project.basePackage+'.'+module.moduleName+'.model.'+model.name?cap_first+method.name?cap_first+'Cmd']>
        <#if !importPkgs?seq_contains('org.springframework.web.bind.annotation.PatchMapping')>
            <#assign importPkgs=importPkgs+['org.springframework.web.bind.annotation.PatchMapping']>
        </#if>
        <#if !importPkgs?seq_contains('org.springframework.web.bind.annotation.RequestBody')>
            <#assign importPkgs=importPkgs+['org.springframework.web.bind.annotation.RequestBody']>
        </#if>
        <#if !importPkgs?seq_contains('jakarta.validation.Valid')>
            <#assign importPkgs=importPkgs+['jakarta.validation.Valid']>
        </#if>
    </#if>
    <#if method.type == "REMOVE_CMD" && primaryField??>
        <#assign importPkgs=importPkgs+['com.fa4j.common.base.model.RemoveCmd','com.fa4j.system.login.util.LoginUtil']>
        <#if !importPkgs?seq_contains('org.springframework.web.bind.annotation.DeleteMapping')>
            <#assign importPkgs=importPkgs+['org.springframework.web.bind.annotation.DeleteMapping']>
        </#if>
        <#if !importPkgs?seq_contains('org.springframework.web.bind.annotation.PathVariable')>
            <#assign importPkgs=importPkgs+['org.springframework.web.bind.annotation.PathVariable']>
        </#if>

    </#if>
</#list>
<#list importPkgs?filter(p->!p?starts_with("java.lang"))?sort as pkg>
import ${pkg};
</#list>

/**
 * Web接口-${model.remark}
 *
 * @since ${.now?string("yyyy-MM-dd HH:mm:ss")}
 */
@RestController
@RequestMapping("${module.moduleName?replace(".","/")}")
@RequiredArgsConstructor
public class ${model.name?cap_first}Controller {

    private final ${model.name?cap_first}Service ${model.name}Service;
    private final ${model.name?cap_first}Repository ${model.name}Repository;

    <#list model.methodList as method>

        <#if method.type == "PAGE_QUERY">
    @GetMapping("${method.name}")
    @ApiMethod(name = "${model.remark}-${method.remark}", type = AppType.MANAGE)
    public Page<${model.name?cap_first}> ${method.name}(${model.name?cap_first}${method.name?cap_first}Query query) {
        return ${model.name}Repository.queryPage(query);
    }
        </#if>
        <#if method.type == "DETAIL_QUERY" && primaryField??>
    @GetMapping("{id}")
    @ApiMethod(name = "${model.remark}-${method.remark}", type = AppType.MANAGE)
    public ${model.name?cap_first} ${method.name}(@PathVariable ${primaryField.type.type.simpleName} ${primaryField.name}) {
        return ${model.name}Repository.findById(${primaryField.name})
                .orElseThrow(() -> CommonRequestError.NOT_FOUND.build("${model.remark}"));
    }
        </#if>
        <#if method.type == "CREATE_CMD">
    @PostMapping
    @ApiMethod(name = "${model.remark}-${method.remark}", type = AppType.MANAGE)
    public ${model.name?cap_first} ${method.name}(@Valid @RequestBody ${model.name?cap_first}${method.name?cap_first}Cmd cmd) {
        return ${model.name}Service.${method.name}(cmd);
    }
        </#if>
        <#if method.type == "MODIFY_CMD" && primaryField??>
    @PatchMapping
    @ApiMethod(name = "${model.remark}-${method.remark}", type = AppType.MANAGE)
    public ${model.name?cap_first} ${method.name}(@Valid @RequestBody ${model.name?cap_first}${method.name?cap_first}Cmd cmd) {
        return ${model.name}Service.${method.name}(cmd);
    }
        </#if>
        <#if method.type == "REMOVE_CMD" && primaryField??>
    @DeleteMapping("{id}")
    @ApiMethod(name = "${model.remark}-${method.remark}", type = AppType.MANAGE)
    public ${model.name?cap_first} remove(@PathVariable ${primaryField.type.type.simpleName} ${primaryField.name}) {
        return ${model.name}Service.${method.name}(RemoveCmd.of(${primaryField.name}, LoginUtil.getAccountId()));
    }
        </#if>
    </#list>
}

